
	//页面加载的时候就进行查询
	$(function(){

		var screenwidth, screenheight, mytop, getPosLeft, getPosTop
		screenwidth = $(window).width();
		screenheight = $(window).height();
		//获取滚动条距顶部的偏移
		mytop = $(document).scrollTop();
		//计算弹出层的left
		getPosLeft = screenwidth / 2 - 260;
		//计算弹出层的top
		getPosTop = screenheight / 2 - 150;
		//css定位弹出层
		$("#deptTree").css({ "left": getPosLeft, "top": getPosTop });
		$("#deploy").css({ "left": getPosLeft, "top": getPosTop });
		$("#bathDeploy").css({ "left": getPosLeft, "top": getPosTop });
		//当浏览器窗口大小改变时
		$(window).resize(function() {
			screenwidth = $(window).width();
			screenheight = $(window).height();
			mytop = $(document).scrollTop();
			getPosLeft = screenwidth / 2 - 200;
			getPosTop = screenheight / 2 - 150;
			$("#deptTree").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
			$("#deploy").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
			$("#bathDeploy").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
		});
		//当拉动滚动条时，弹出层跟着移动
		$(window).scroll(function() {
			screenwidth = $(window).width();
			screenheight = $(window).height();
			mytop = $(document).scrollTop();
			getPosLeft = screenwidth / 2 - 200;
			getPosTop = screenheight / 2 - 150;
			$("#deptTree").css({ "left": getPosLeft, "top": getPosTop+ mytop  });	
			$("#deploy").css({ "left": getPosLeft, "top": getPosTop+ mytop  });	
			$("#bathDeploy").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
		}); 
		 //获取页面文档的高度
		 var docheight = $(document).height();
		//追加一个层，使背景变灰
		$("body").append("<div id='greyBackGround'></div>");
		$("body").append("<div id='deptTreeBackGround'></div>");
		$("#greyBackGround").css({ "opacity": "0.1", "height": docheight });
		$("#deptTreeBackGround").css({ "opacity": "0.1", "height": docheight });
		
		
		
		$("#deployBtn").click(function(){
			var tempThis=$(this);
			var deployDeptName = $("#tempDeptId")[0].value;
			var deployEmpName = $("#deployEmpNo option:selected").text();
			$("#deployDeptName")[0].value=deployDeptName;
			$("#deployEmpName")[0].value=deployEmpName;
			var reason = $("#reason")[0].value;
			if(reason==null || reason==""){
				alert("请填写原因!");
				return;
			}
			var deployDeptId = $("#deployDeptId")[0].value;
			if(deployDeptId==null || deployDeptId==""){
				alert("请选择部门!");
				return;
			}
			
			 $.ajax({
	             type: "POST",
	             dataType:"text",
	             url:getContextPathTemp()+"/ruleSet/customDeploy/checkIsCanDeploy",
	             data:"deployDeptId="+deployDeptId,
	             async: false,
	             success: function(data){
	            	 if(data=="true"){
	            		 //$("#deployForm").submit();
	            			var url = getContextPathTemp()+"/ruleSet/customDeploy/customDeploy";
	            			 $.ajax({
	            		         type: "POST",
	            		         dataType:"json",
	            		         url:url,
	            		         data:$("#deployForm").serialize(),
	            		         success: function(data){
	            		        	 if(data=="1"){
	            		        		 alert("调配成功!");
	            		        		 var cusId=$("#cusId").val();
	            		        		 $("#"+cusId+"stateLabel").attr("href","#");
	            		        		 $("#"+cusId+"stateLabel").html("审核中");
	            		        		 $("#deploy").fadeOut(100);
	            		        		 $("#greyBackGround").css("display","none");
	            		        	 }else if(data=="0"){
	            		        		 alert("已经有其他人调配此记录!"); 
	            		        	 }else if(data=="3"){
	            		        		 alert("调配异常!"); 
	            		        	 }
//	            		        	 $("#redirectForm").submit(); 
	            		         }
	            		     }); 
	            		 
	            	 }else{
	            		alert("此部门已没有该用户使用的产品，请选择其他部门!"); 
	            	 }
	             }
	         });
		});
		
		$("#deployBtnCancel").click(function(){
			 $("#greyBackGround").css("display","none");
			 $("#deploy").css("display","none");
			 $("#cusId")[0].value="";
			 $("#deployCName")[0].value="";
			 $("#deployMail")[0].value="";
			 $("#deployPhone")[0].value="";
			 $("#deployCurrentDeptNo")[0].value="";
			 $("#deployCurrentEmpNo")[0].value="";
			 $("#deployCurrentDeptName")[0].value="";
			 $("#deployCurrentEmpName")[0].value="";
			 $("#deployDeptName")[0].value="";
			 $("#deployEmpName")[0].value="";
			 
			 $("#productId")[0].value="";
			 $("#productCode")[0].value="";
			 $("#deployCurrentDeptNameLabel").html();
			 $("#deployCurrentEmpNameLabel").html();
			 
			 $("#reason")[0].value="";
			 $("#deployEmpNo").empty();
			 $("#deployEmpNo").append("<option value='' selected='selected'>---请选择---</option>");
			 $("#deployDeptId")[0].value="";
			 $("#tempDeptId")[0].value="";
			 
		});
		
		$("#bathDeployBtn").click(function(){
			 $("#greyBackGround").css("display","none");
			 $("#bathDeploy").css("display","none");
			
		});
		$("#bathDeployBtnCancel").click(function(){
			 $("#greyBackGround").css("display","none");
			 $("#bathDeploy").css("display","none");
			
			
			
		});
		
		
		$("#deployDeptId").change(function(){
			var deptId =  $("#deployDeptId").val(); 
			var deployCurrentEmpNo =  $("#deployCurrentEmpNo").val();
			//alert("deployCurrentEmpNo:"+deployCurrentEmpNo);
			 $.ajax({
	             type: "POST",
	             url:getContextPathTemp()+"/ruleSet/customDeploy/getEmpByDeptId",
	             data:"deptId="+deptId+"&currentEmpId="+deployCurrentEmpNo,
	             async: false,
	             error: function(request){
	                 alert("Connection error");
	                 location.reload();
	             },
	             success: function(data){
	             	$("#deployEmpNo").empty();
	            	$("#deployEmpNo").append("<option value='' selected='selected'>---请选择---</option>");
	            	 if(data!=""){
	                 	var empList = eval(data);
	                	if(empList!=""){
	                    	for( var int = 0; int < empList.length; int++){
	                    		var emp = empList[int];
	                    		var empno = emp.empno;
	                    		var ename = emp.ename;
	                    		$("#deployEmpNo").append("<option value='"+empno+"'>"+ename+"</option>");
	        				}
	                	}
	            	 }
	             }
	         }); 
		});
		
		$("#queryBtn").click(function(){
			//var tempDeptId = $("#queryTempDeptId")[0].value;
			//$("#queryDeptId")[0].value=tempDeptId;
			$("#queryForm").submit();
		});
		
		
		
		
		$("#bathDeployBtn").click(function(){
			//var deployDeptName = $("#deployDeptId option:selected").text();
			var deployDeptName = $("#tempDeptId")[0].value;
			//alert("deployDeptName: "+ deployDeptName);
			var deployEmpName = $("#deployEmpNo option:selected").text();
			$("#deployDeptName")[0].value=deployDeptName;
			$("#deployEmpName")[0].value=deployEmpName;
			var reason = $("#reason")[0].value;
			if(reason==null || reason==""){
				alert("请填写原因!");
				return;
			}
			var deployDeptId = $("#deployDeptId")[0].value;
			if(deployDeptId==null || deployDeptId==""){
				alert("请选择部门!");
				return;
			}
			 $.ajax({
	             type: "POST",
	             url:getContextPathTemp()+"/ruleSet/customDeploy/checkIsCanDeploy",
	             data:"deployDeptId="+deployDeptId,
	             async: false,
	             success: function(data){
	            	 var result = eval(data);
	            	 if(result){
	            			//$("#deployForm").attr("action", "../../ruleSet/customDeploy/bathCustomDeploy");
	            			//$("#deployForm").submit(); 
	            			
	               			var url = getContextPathTemp()+"/ruleSet/customDeploy/bathCustomDeploy";
	               			 $.ajax({
	               		         type: "POST",
	               		         dataType:"json",
	               		         url:url,
	               		         data:$("#deployForm").serialize(),
	               		         async: false,
	               		         success: function(data){
	               		        	 if(data=="1"){
	               		        		 alert("调配申请成功!"); 
	               		        	 }else if(data=="0"){
	               		        		 alert("调配记录中,存在已被其他人调配过的记录,请重新选择!"); 
	               		        	 }
	               		        	 $("#redirectForm").submit(); 
	               		         }
	               		     }); 	
	            	 }else{
	            		alert("此部门已没有该用户使用的产品，请选择其他部门!"); 
	            	 }
	             }
	         });
		});
		
		$("#bathDeployBtnCancel").click(function(){
			
			$("#deployBtn").css("display","block");
			$("#deployBtnCancel").css("display","block");
			$("#bathDeployBtn").css("display","none");
			$("#bathDeployBtnCancel").css("display","none");
			
			$("#cusId")[0].value="";
			$("#deployCName")[0].value="";
			$("#deployMail")[0].value="";
			$("#deployPhone")[0].value="";
			$("#deployCurrentDeptNo")[0].value="";
			$("#deployCurrentEmpNo")[0].value="";
			$("#deployCurrentDeptName")[0].value="";
			$("#deployCurrentEmpName")[0].value="";
			$("#deployDeptName")[0].value="";
			$("#deployEmpName")[0].value="";
			$("#deployCurrentDeptNameLabel").html();
			$("#deployCurrentEmpNameLabel").html();
			$("#reason")[0].value="";
			$("#deployEmpNo").empty();
			$("#deployEmpNo").append("<option value='' selected='selected'>---请选择---</option>");
			$("#deployDeptId")[0].value="";
		});
		
		initTree();
		//初始化所属部门树
		var setting = {	
				view: {selectedMulti:false},data: {keep: {parent:true,leaf:true},simpleData: {enable: true}},
				callback: {beforeDrag: beforeDrag}
		};
		function beforeDrag(treeId, treeNodes){
			return false;
		}
		$.fn.zTree.init($("#treeDemo1"), setting, zNodes);
		
		$("#deptTreeBtnCancel").click(function(){
		 	$("#deptTreeBackGround").css("display","none");
		 	$("#deptTree").css("display","none");
		});
		$("#deptTreeBtnConfirm").click(function(){
			var selectedNodeId="";
			var selectedNodeName="";
	        var treeObj=$.fn.zTree.getZTreeObj("treeDemo1");
	        nodes=treeObj.getSelectedNodes();
			 for ( var int = 0; int < nodes.length; int++){
				 var node = nodes[int];
				 selectedNodeId = node.id;
				 selectedNodeName = node.name;
				 break;
			} 
	        if(selectedNodeId==""){
	        	alert("请选择部门!");
	        	return;
	        }
			$("#deployDeptId").val(selectedNodeId);
			$("#tempDeptId").val(selectedNodeName);
			$("#deployDeptName").val(selectedNodeName);
			
			var deployCurrentEmpNo =  $("#deployCurrentEmpNo").val();
			//alert("deployCurrentEmpNo:"+deployCurrentEmpNo);
			if(selectedNodeId!=null && selectedNodeId!=""){
				 $.ajax({
		             type: "POST",
		             url:getContextPathTemp()+"/ruleSet/customDeploy/getEmpByDeptId",
		             data:"deptId="+selectedNodeId+"&currentEmpId="+deployCurrentEmpNo,
		             async: false,
		             success: function(data){
		             	$("#deployEmpNo").empty();
		            	$("#deployEmpNo").append("<option value='' selected='selected'>---请选择---</option>");
		            	 if(data!=""){
		                 	var empList = eval(data);
		                	if(empList!=""){
		                    	for( var int = 0; int < empList.length; int++){
		                    		var emp = empList[int];
		                    		var empno = emp.empno;
		                    		var ename = emp.ename;
		                    		$("#deployEmpNo").append("<option value='"+empno+"'>"+ename+"</option>");
		        				}
		                	}
		            	 }
		             }
		         }); 
			}
		 	$("#deptTreeBackGround").css("display","none");
		 	$("#deptTree").css("display","none");
		});
		
		
		
		
		$("#queryDeptTreeBtnConfirm").click(function(){
			var selectedNodeId="";
			var selectedNodeName="";
	        var treeObj=$.fn.zTree.getZTreeObj("treeDemo1");
	        nodes=treeObj.getSelectedNodes();
			 for ( var int = 0; int < nodes.length; int++){
				 var node = nodes[int];
				 selectedNodeId = node.id;
				 selectedNodeName = node.name;
				 break;
			} 
	        if(selectedNodeId==""){
	        	alert("请选择部门!");
	        	return;
	        }
			$("#queryDeptId").val(selectedNodeId);
			$("#queryTempDeptId").val(selectedNodeName);
			if(selectedNodeId!=null && selectedNodeId!=""){
				 $.ajax({
		             type: "POST",
		             url:getContextPathTemp()+"/ruleSet/customDeploy/getEmpAndAreaByDeptId",
		             data:"deptId="+selectedNodeId,
		             async: false,
		             success: function(data){
		             	$("#queryEmpName").empty();
		            	$("#queryEmpName").append("<option value='' selected='selected'>---请选择---</option>");
		            	 if(data!=""){
		                 	var result = eval(data);
		                 	var areaList = result[0].areaList;
		                 	var empList = result[0].empList;
		                	if(empList!=""){
		                    	for( var int = 0; int < empList.length; int++){
		                    		var emp = empList[int];
		                    		var empno = emp.empno;
		                    		var ename = emp.ename;
		                    		$("#queryEmpName").append("<option value='"+ename+"'>"+ename+"</option>");
		        				}
		                	}
		                	if(areaList!=""){
		                    	for( var int = 0; int < areaList.length; int++){
		                    		var area = areaList[int];
		                    		var areaID = area.areaID;
		                    		var name =  area.name;
		                    		$("#queryArea").append("<option value='"+name+"'>"+name+"</option>");
		        				}
		                	}
		            	 }
		             }
		         }); 
			}
		 	$("#deptTreeBackGround").css("display","none");
		 	$("#deptTree").css("display","none");
		});
		
		
		//清空			 		 
		$("#clean").click(function(){
			$("#customerId")[0].value="";
			$("#contact")[0].value="";
			$("#phone")[0].value="";
			$("#email").val("");
			$("#queryDeptId").val("");
			$("#queryTempDeptId").val("");
			$("#queryEmpName").val("");
			$("#queryArea").val("");
		});
		
		query(1);
	});

//查询即将到期客户
function query(pageNow) {
	controllerWaitingBar(true);
	//获取项目上下文路径
	rootPath = $('#rootPath').val();
	//获取要查询的客户类型
	var customerType=$("#customerType").val();
	
	//获取当前页的下一页/上一页
	if(!pageNow) {
		pageNow = 1;
	}else if(pageNow == "nextPage") {
		pageNow = $('#pageNow').val();
		var pageCount = $('#pageCount').html();
		//如果当前页是最后一页 则下一页无效
		if(pageNow == pageCount) {
			controllerWaitingBar(false);
			return ;
		}
		pageNow = $('#pageNow').val()*1 + 1;
	}else if(pageNow == "prevPage") {
		pageNow = $('#pageNow').val();
		//如果当前页是第一页 则上一页无效
		if(pageNow == "1") {
			controllerWaitingBar(false);
			return ;
		}
		pageNow = $('#pageNow').val()*1 - 1;
	}else if(pageNow == "lastPage") {
		pageNow = $('#pageCount').html();
	}
	
	var productLabel = $('#productLabel').val();
	var startDueTime = $('#startTime').val();
	var endDueTime = $('#endTime').val();
	var customId = $('#customId').val();
	var customName = $('#customName').val();
	var phone = $('#phone').val();
	var email = $('#email').val();
	var empId = $('#emp').val();
	var deptId = $('#deptId').val();
	var regionalId = $('#area').val();
	if(!checkForm()) {
		return ;
	}
	
	var url = rootPath + "/customerManage/queryDueSoonGoods";

	$.post(
		url,
		{
			"productLabel": productLabel,
			"startDueTime": startDueTime,
			"endDueTime": endDueTime,
			"customId": customId,
			"customName": customName,
			"phone": phone,
			"email": email,
			"empId": empId,
			"deptId": deptId,
			"regionalId": regionalId,
			"customerType": customerType,
			"pageNow": pageNow
		},
		function(data) {
			var json = eval(data);
			if(5 == json) {
				location.href = rootPath + "/login/loginPage";
				return ;
			}
			var html = "";
			html += '<tr>';
			html += '<th>选择&nbsp;<input type="checkbox" onclick="selectedBox(this)" /></th>';
			html += '<th>会员编号</th>';
			html += '<th>服务编号</th>';
			html += '<th>商品类型</th>';
			html += '<th>商品名称</th>';
			html += '<th>商品价格</th>';
			html += '<th>域名</th>';
	        html += '<th>所属人</th>';
	        html += '<th>服务期开始日期</th>';
	        html += '<th>服务期到期日期</th>';   
	        html += '<th>操作</th>';   
	        html += '</tr>';
	        //var isHasCustomAlot =  $('#isHasCustomAlot').val();
	        var isResponsible =  $('#isResponsible').val();
	        var userId = $('#userId').val();
	        var currentdid = $('#currentdid').val();
	        if(currentdid=="001"){
	        	isResponsible=0;
	        }
	        if(json==undefined || 0== json.length) {
				
				html += '<tr><td colspan="16">没有查到相关数据！</td></tr>';
			}else {
				
				for(var i=0; i<json.length; i++) {
					var customState = json[i].customState;
					html += '<tr>';
					var checkbox = "";
					/*if(customState != 0 && ((json[i].approveStatus!=null && json[i].approveStatus==3) || (json[i].approveStatus!=null && json[i].approveStatus==1 || json[i].approveStatus==0) || (json[i].approveStatus == null)) ){
						checkbox ='<td><input id="multiChoice_'+json[i].ecrId+'" type="checkbox" value="'+
						json[i].ecrId +':'+
						json[i].customId +':'+
						json[i].empId +':'+
						json[i].productId +':'+
						'"  /></td>';
					} else {
						
						if(userId == json[i].empId && customState == 0) {
							checkbox ='<td><input type="checkbox" value="'+
							json[i].ecrId +':'+
							json[i].customId +':'+
							json[i].empId +':'+
							json[i].productId +':'+
							'"  /></td>';
						}else {
							checkbox="<td></td>";
						}
						
					}*/
					if(userId == json[i].empId && customState == 0) {
						checkbox ='<td><input type="checkbox" value="'+
						json[i].ecrId +':'+
						json[i].customId +':'+
						json[i].empId +':'+
						json[i].productId +':'+
						'"  /></td>';
					}else {
						checkbox="<td></td>";
					}
					html += checkbox;
					
					html += '<td><a href="#" onclick="goCustomerDetails(' + "'" + json[i].customId + "'" + ')">' + json[i].customId + '</a></td>';
					html += '<td>' + json[i].serviceId + '</td>';
					html += '<td>' + json[i].productName + '</td>';
					html += '<td>' + json[i].goodsName + '</td>';
					html += '<td>' + getNum(json[i].goodsPrice,3) + '</td>';
					html += '<td>' + json[i].domainName + '</td>';
					html += '<td>' + json[i].empName + '</td>';
					html += '<td>' + json[i].buyTime + '</td>';
					html += '<td>' + json[i].dueTime + '</td>';
					html += '<td></td>';
					
					html += '</tr>';
				}
			}
			$('#dataList').html(html);
			
			//显示分页
			if(json.length) {
				var pageNow = json[0].pageNow;
				var pageSize = json[0].pageSize;
				var rowCount = json[0].rowCount;
				var pageCount = json[0].pageCount;
				
				$('#pageNow').val(pageNow);
				$('#pageSize').html("1-" + pageSize);
				$('#rowCount').html(rowCount);
				$('#pageIndex').html(pageNow);
				$('#pageCount').html(pageCount);
			}else {
				$('#pageNow').val("1");
				$('#pageSize').html("1-50");
				$('#rowCount').html("0");
				$('#pageIndex').html("1");
				$('#pageCount').html("1");
			}
			controllerWaitingBar(false);
		}
	);
}

function getNum(num,count)  
{   
return num.substring(0,num.indexOf(".")+count);  
}  

function customDeploy(ecrId,cmaId,cusId,deptno,dname,empno,ename,cname,phone,email,productId,productCode){
	//alert("ecrId: "+ ecrId);
	//alert("cmaId: "+ cmaId);
	 $("#ecrId")[0].value=ecrId;
	 $("#cmaId")[0].value=cmaId;
	 $("#cusId")[0].value=cusId;
	 $("#deployCName")[0].value=cname;
	 $("#deployMail")[0].value=email;
	 $("#deployPhone")[0].value=phone;
	 $("#deployCurrentDeptNo")[0].value=deptno;
	 $("#deployCurrentEmpNo")[0].value=empno;
	 $("#deployCurrentDeptName")[0].value=dname;
	 $("#deployCurrentEmpName")[0].value=ename;
	 $("#productId")[0].value=productId;
	 $("#productCodeDeploy")[0].value=productCode;
	 $("#deployDeptName")[0].value=$("#deployDeptId").text();
	 $("#deployEmpName")[0].value=$("#deployEmpNo").text();
	 $("#deployCurrentDeptNameLabel").html(dname);
	 $("#deployCurrentEmpNameLabel").html(ename);
	 
   	 $("#greyBackGround").css("display","block");
	 $("#deploy").fadeIn("slow"); 
}

function bathCustomDeploy(){
	var ecrIdStr="";
	var list = $("input[id^=multiChoice_][type='checkbox']:checked");
	if(list.length>0){
		for(var int = 0; int < list.length; int++){
			var id = list[int].id.replace("multiChoice_","");
			if(ecrIdStr==""){
				ecrIdStr=id;
			}else{
				ecrIdStr = ecrIdStr+","+id;
			}	
		}
	}
	if(ecrIdStr==""){
		alert("请选择记录!");
		return;
	}
	
	$("#ecrIdStr").val(ecrIdStr);
	$("#deployBtn").css("display","none");
	$("#bathDeployBtn").css("display","block");

	$("#deployCName")[0].value="";
	$("#deployMail")[0].value="";
	$("#deployPhone")[0].value="";
	$("#deployCurrentDeptNo")[0].value="";
	$("#deployCurrentEmpNo")[0].value="";
	$("#deployCurrentDeptName")[0].value="";
	$("#deployCurrentEmpName")[0].value="";
	$("#deployDeptName")[0].value="";
	$("#deployEmpName")[0].value="";
	$("#deployCurrentDeptNameLabel").html();
	$("#deployCurrentEmpNameLabel").html();
	$("#reason")[0].value="";
	$("#deployEmpNo").empty();
	$("#deployEmpNo").append("<option value='' selected='selected'>---请选择---</option>");
	$("#deployDeptId")[0].value="";

	$("#greyBackGround").css("display","block");
	$("#deploy").fadeIn("slow");
}

function displayDeptTree(){
 	 $("#deptTreeBackGround").css("display","block");
 	 $("#queryDeptTreeBtnConfirm").css("display","none");
 	 $("#deptTreeBtnConfirm").css("display","block");
	 $("#deptTree").fadeIn("slow"); 
}
function queryDeptTree(){
	 $("#deptTreeBtnConfirm").css("display","none");
	 $("#queryDeptTreeBtnConfirm").css("display","block");
	 $("#deptTreeBackGround").css("display","block");
	 $("#deptTree").fadeIn("slow"); 
}

function selectAll(){
	if($("#selectAll").attr("checked")==true){
		$('input[id^=multiChoice_]').attr("checked",true); 
	}else{
		$('input[id^=multiChoice_]').attr("checked",false); 
		
	}
}


function getContextPathTemp() {
	var result=$("#rootPath").val();
    return result;
}










